                                                                     
                                                                     
                                                                     
                                             
--
-- VHDL Architecture Fietscomputer_lib.i_serienummer.v1
--
-- Created:
--          by - Patrick.UNKNOWN (dtp7985)
--          at - 14:59:39  29-09-2011
--
LIBRARY ieee;
USE ieee.std_logic_1164.all;
--USE ieee.std_logic_arith.all;
--USE ieee.std_logic_UNSIGNED.all; 
use ieee.numeric_std.all;


ENTITY fc_Trip IS
  PORT( 
  rst           	: IN     STD_LOGIC;
  clk           	: IN     STD_LOGIC;
  
  Time_Out      	: IN     STD_LOGIC;
  Start_Trip      	: IN     STD_LOGIC;	
  End_Trip			: IN     STD_LOGIC;
  
  Delta_T_Strobe 	: IN     STD_LOGIC;
  Dist_const    	: IN     STD_LOGIC_VECTOR(63 DOWNTO 0);
  
  Time_Ready      	: OUT    STD_LOGIC;    
  Dist_Speed_Ready      	: OUT    STD_LOGIC;    
  
  Time_Div_Const	: OUT STD_LOGIC_VECTOR(63 DOWNTO 0);   
  Trip_Time     	: BUFFER STD_LOGIC_VECTOR(63 DOWNTO 0);
  
  
  Trip_Dist     	: BUFFER STD_LOGIC_VECTOR(63 DOWNTO 0);
  Dist_Div_Const	: OUT STD_LOGIC_VECTOR(63 DOWNTO 0)
  
);
END fc_Trip ;


ARCHITECTURE v OF fc_Trip IS  
SIGNAL counter : NATURAL RANGE 0 TO 10001;
SIGNAL timed_out : STD_LOGIC;
SIGNAL trip_running : STD_LOGIC;

BEGIN
  
  Time_Div_Const <= STD_LOGIC_VECTOR(  to_unsigned(10000, Time_Div_Const'LENGTH  ) );	
  Dist_Div_Const <= STD_LOGIC_VECTOR( to_unsigned(36000000, Dist_Div_Const'LENGTH) );

--Process om de trip tijd te bepalen	
PROCESS (clk, rst) BEGIN
  IF rst = '1' THEN
  counter <= 0;
  Trip_Time <= (OTHERS => '0');  
  Time_Ready <= '0';
  ELSIF rising_edge(clk) THEN	
  Time_Ready <= '0';
    IF Start_Trip = '1' THEN
      Trip_Time <= (OTHERS => '0');
      Time_Ready <= '1';
      counter <= 0;
  ELSIF End_Trip = '1' THEN
    --Trip_Time <= (OTHERS => '0');
      Time_Ready <= '1';
  ELSIF trip_running = '1' THEN  
    IF timed_out = '0' THEN
         counter <= counter +1;
       Trip_Time <= STD_LOGIC_VECTOR( UNSIGNED(Trip_Time) + 1);
       IF counter = 10000 THEN
         counter <= 0;
         Time_Ready<= '1';
       END IF;			   
    END IF;

  END IF;
  END IF;
END PROCESS;

  
-- TimeOut afhandelen
PROCESS (clk, rst) 
BEGIN 
  IF rst = '1' THEN
  timed_out <= '0';
  ELSIF rising_edge(clk) THEN
  IF Delta_T_Strobe = '1' THEN
    timed_out <= '0';
  ELSIF Time_Out = '1' THEN
    timed_out <= '1';
  END IF;
  END IF;
END PROCESS; 


-- Trip_Running afhandelen
PROCESS (clk, rst) 
BEGIN 
  IF rst = '1' THEN
  trip_running <= '0';
  ELSIF rising_edge(clk) THEN
  IF Start_Trip = '1' THEN
    trip_running <= '1';
  ELSIF End_Trip = '1' THEN
    trip_running <= '0';
  END IF;
  END IF;
END PROCESS;
  
--Process om de trip afstand te bepalen	
PROCESS (clk, rst) BEGIN
  IF rst = '1' THEN
      Dist_Speed_Ready <= '0';
    Trip_Dist <= (OTHERS => '0');
  ELSIF rising_edge(clk) THEN	
    Dist_Speed_Ready <= '0';
    IF Start_Trip = '1' THEN
      Trip_Dist <= (OTHERS => '0');
      Dist_Speed_Ready <= '1';
    ELSIF End_Trip = '1' THEN
      --Trip_Dist <= (OTHERS => '0');
        Dist_Speed_Ready <= '1';
    ELSIF trip_running = '1' THEN
        IF Delta_T_Strobe = '1' THEN
          IF timed_out = '0' THEN
            Trip_Dist <= STD_LOGIC_VECTOR( UNSIGNED(Trip_Dist) + UNSIGNED(Dist_const));
          END IF;
          Dist_Speed_Ready <= '1';
        END IF;		
    END IF;
  END IF;
END PROCESS;




END ARCHITECTURE v;